﻿using System.Data;
using Think9.CreatCode;
using Think9.Services.Base;

namespace Think9.Services.Basic
{
	public class AppSet
	{
		public static TableDtModel GetTableDtModel(string maintbid)
		{
			TableDtModel model = new TableDtModel();
			ComService comService = new ComService();

			string sql = "select * from TbEventCustomize order by TbId,OrderNo";
			DataTable dtTbEventCustomize = comService.GetDataTable(sql);
			model.dtTbEventCustomize = dtTbEventCustomize;

			sql = "select * from TbIndex order by TbId,IndexOrderNo";
			DataTable _dtindex = comService.GetDataTable(sql);
			model.dtIndex = _dtindex;

			sql = "select * from tbbasic where ParentId='" + maintbid + "'";
			DataTable sonlist = comService.GetDataTable(sql);
			model.sonList = sonlist;

			sql = "select * from tbbasic ";
			DataTable allTB = comService.GetDataTable(sql);
			model.allTB = allTB;

			sql = "select * from tbbasic2 ";
			DataTable allTB2 = comService.GetDataTable(sql);
			model.allTB2 = allTB2;

			sql = "select * from rulesingle";
			DataTable dtrulesingle = comService.GetDataTable(sql);
			model.dtRuleSingle = dtrulesingle;

			sql = "select * from RuleMultiple";
			DataTable dtrulemultiple = comService.GetDataTable(sql);
			model.dtRuleMultiple = dtrulemultiple;

			sql = "select * from RuleMultiplefiled order by RuleId,FiledOrder";
			DataTable dtrulemultiplefiled = comService.GetDataTable(sql);
			model.dtRuleMultipleFiled = dtrulemultiplefiled;

			sql = "select * from ruletree";
			DataTable dtruletree = comService.GetDataTable(sql);
			model.dtRuleTree = dtruletree;

			sql = "select * from flow";
			DataTable dtflow = comService.GetDataTable(sql);
			model.dtFlow = dtflow;

			sql = "select * from flowprcs order by FlowId,PrcsNo";
			DataTable dtflowprcs = comService.GetDataTable(sql);
			model.dtFlowPrcs = dtflowprcs;

			sql = "select * from tbbut order by TbId";
			DataTable dtTbBut = comService.GetDataTable(sql);
			model.dtTbBut = dtTbBut;

			sql = "SELECT TbRelation.*,RelationList.* FROM  TbRelation INNER JOIN RelationList ON TbRelation.RelationId = RelationList.RelationId  ";
			DataTable dtVIEW_TbRelationRD = comService.GetDataTable(sql);
			model.dtVIEW_TbRelationRD = dtVIEW_TbRelationRD;

			sql = "select * from RelationRD order by RelationId";
			DataTable dtRelationRD = comService.GetDataTable(sql);
			model.dtRelationRD = dtRelationRD;

			sql = "select * from RelationRDField order by ID, FillIndexId";//order by ID很重要--对数据联动
            DataTable dtRelationRDField = comService.GetDataTable(sql);
			model.dtRelationRDField = dtRelationRDField;

			sql = @"SELECT RelationList.RelationId, RelationList.RelationType, RelationList.RelationName, RelationList.RelationBy, RelationList.RelationFlag, RelationList.FlowStr, RelationList.ICount,  RelationWD.SourceFlowId, RelationWD.SourceTbId, RelationWD.FlowPrcs, RelationWD.WriteTbId,RelationWD.RelationWDFlag, RelationWD.OrderNo, RelationWD.RelationWDBy, TbRelation.TbID,TbRelation.FlowScope, RelationWD.WhereStr, RelationWD.NumberType, RelationWD.RelationSort,RelationList.DbID FROM  RelationList INNER JOIN RelationWD ON RelationList.RelationId = RelationWD.RelationId INNER JOIN TbRelation ON RelationList.RelationId = TbRelation.RelationId  where TbRelation.isUse='1' order by RelationId";

			DataTable dtVIEW_TbRelationWD = comService.GetDataTable(sql);
			model.dtVIEW_TbRelationWD = dtVIEW_TbRelationWD;

			sql = @"select * from RelationWDField";
			DataTable dtRelationWDField = comService.GetDataTable(sql);
			model.dtRelationWDField = dtRelationWDField;

			return model;
		}

		public static TableDtModel GetTableDtModel()
		{
			TableDtModel model = new TableDtModel();

			ComService comService = new ComService();

			string sql = "select * from TbEventCustomize order by TbId,OrderNo";
			DataTable dtTbEventCustomize = comService.GetDataTable(sql);
			model.dtTbEventCustomize = dtTbEventCustomize;

			sql = "select * from TbIndex order by TbId,IndexOrderNo";
			DataTable _dtindex = comService.GetDataTable(sql);
			model.dtIndex = _dtindex;

			sql = "select * from RuleList";
			model.dtRuleList = comService.GetDataTable(sql);

			sql = "select * from tbbasic ";
			DataTable allTB = comService.GetDataTable(sql);
			model.allTB = allTB;

			sql = "select * from tbbasic2 ";
			DataTable allTB2 = comService.GetDataTable(sql);
			model.allTB2 = allTB2;

			sql = "select * from rulesingle";
			DataTable dtrulesingle = comService.GetDataTable(sql);
			model.dtRuleSingle = dtrulesingle;

			sql = "select * from RuleMultiple";
			DataTable dtrulemultiple = comService.GetDataTable(sql);
			model.dtRuleMultiple = dtrulemultiple;

			sql = "select * from RuleMultiplefiled order by RuleId,FiledOrder";
			DataTable dtrulemultiplefiled = comService.GetDataTable(sql);
			model.dtRuleMultipleFiled = dtrulemultiplefiled;

			sql = "select * from ruletree";
			DataTable dtruletree = comService.GetDataTable(sql);
			model.dtRuleTree = dtruletree;

			sql = "select * from flow";
			DataTable dtflow = comService.GetDataTable(sql);
			model.dtFlow = dtflow;

			sql = "select * from flowprcs order by FlowId,PrcsNo";
			DataTable dtflowprcs = comService.GetDataTable(sql);
			model.dtFlowPrcs = dtflowprcs;

			sql = "select * from tbbut order by TbId";
			DataTable dtTbBut = comService.GetDataTable(sql);
			model.dtTbBut = dtTbBut;

			sql = "SELECT TbRelation.*,RelationList.* FROM  TbRelation INNER JOIN RelationList ON TbRelation.RelationId = RelationList.RelationId  ";
			DataTable dtVIEW_TbRelationRD = comService.GetDataTable(sql);
			model.dtVIEW_TbRelationRD = dtVIEW_TbRelationRD;

			sql = "select * from RelationRD order by RelationId";
			DataTable dtRelationRD = comService.GetDataTable(sql);
			model.dtRelationRD = dtRelationRD;

			sql = "select * from RelationRDField order by ID, FillIndexId";//order by ID很重要--对数据联动
            DataTable dtRelationRDField = comService.GetDataTable(sql);
			model.dtRelationRDField = dtRelationRDField;

			sql = @"SELECT RelationList.RelationId, RelationList.RelationType, RelationList.RelationName, RelationList.RelationBy, RelationList.RelationFlag, RelationList.FlowStr, RelationList.ICount,  RelationWD.SourceFlowId, RelationWD.SourceTbId, RelationWD.FlowPrcs, RelationWD.WriteTbId,RelationWD.RelationWDFlag, RelationWD.OrderNo, RelationWD.RelationWDBy, TbRelation.TbID,TbRelation.FlowScope, RelationWD.WhereStr, RelationWD.NumberType, RelationWD.RelationSort,RelationList.DbID FROM  RelationList INNER JOIN RelationWD ON RelationList.RelationId = RelationWD.RelationId INNER JOIN TbRelation ON RelationList.RelationId = TbRelation.RelationId  where TbRelation.isUse='1' order by RelationId";
			DataTable dtVIEW_TbRelationWD = comService.GetDataTable(sql);
			model.dtVIEW_TbRelationWD = dtVIEW_TbRelationWD;

			sql = @"select * from RelationWDField";
			DataTable dtRelationWDField = comService.GetDataTable(sql);
			model.dtRelationWDField = dtRelationWDField;

			return model;
		}

		public static ReportDtModel GetReportDtModel(string _dbtype)
		{
			string order = "";
			string sql = "select * from TbIndex order by TbId,IndexOrderNo";
			ComService comService = new ComService();

			ReportDtModel model = new ReportDtModel();

			sql = "select * from TbIndex order by TbId,IndexOrderNo";
			DataTable _dtindex = comService.GetDataTable(sql);
			model.dtIndex = _dtindex;

			sql = "select * from tbbasic ";
			DataTable allTB = comService.GetDataTable(sql);
			model.allTB = allTB;

			sql = "select * from RuleList";
			model.dtRuleList = comService.GetDataTable(sql);

			sql = "select * from rulesingle";
			DataTable dtrulesingle = comService.GetDataTable(sql);
			model.dtRuleSingle = dtrulesingle;

			sql = "select * from RuleMultiple";
			DataTable dtrulemultiple = comService.GetDataTable(sql);
			model.dtRuleMultiple = dtrulemultiple;

			sql = "select * from RuleMultiplefiled order by RuleId,FiledOrder";
			DataTable dtrulemultiplefiled = comService.GetDataTable(sql);
			model.dtRuleMultipleFiled = dtrulemultiplefiled;

			sql = "select * from ruletree";
			DataTable dtruletree = comService.GetDataTable(sql);
			model.dtRuleTree = dtruletree;

			order = "ORDER BY LENGTH(ParmId) DESC";
			if (_dbtype != "mysql")
			{
				order = "ORDER BY LEN(ParmId) DESC";
			}
			sql = "select * from IndexParm ";
			model.IndexParm = comService.GetDataTable(sql + order);

			sql = "select * from ReportParm  ";
			model.ReportParm = comService.GetDataTable(sql);

			order = "ORDER BY LENGTH(IndexId) DESC";
			if (_dbtype != "mysql")
			{
				order = "ORDER BY LEN(IndexId) DESC";
			}
			sql = "select * from IndexStats ";
			model.IndexStats = comService.GetDataTable(sql + order);

			sql = "select * from ReportBasic ";
			model.ReportBasic = comService.GetDataTable(sql);

			sql = "select * from ReportCols  order by ReportId,ColNum ";
			model.ReportCols = comService.GetDataTable(sql);

			sql = "select * from ReportRows order by ReportId,OrderNo ";
			model.ReportRows = comService.GetDataTable(sql);

			sql = "select * from ReportDYNParm";
			model.ReportDYNParm = comService.GetDataTable(sql);

			sql = "select * from ReportParmQuery order by ReportId,OrderNo ";
			model.ReportParmQuery = comService.GetDataTable(sql);

			sql = "select * from ReportParmQuerySelect";
			model.ReportParmQuerySelect = comService.GetDataTable(sql);

			sql = @"SELECT ReportParm.ReportId, ReportParm.ColNum,ReportParm.RowId,ReportParm.ParmId, ReportParm.ParmValue,ReportRows.Type,ReportRows.OrderNo, ReportRows.DynamicId,ReportRows.I1, ReportRows.I2, ReportRows.I3, ReportRows.I4, ReportRows.I5, ReportRows.I6,ReportRows.I7, ReportRows.I8, ReportRows.I9, ReportRows.I10, ReportRows.I11,ReportRows.I12, ReportRows.I13, ReportRows.I14, ReportRows.I15, ReportRows.I16,ReportRows.I17, ReportRows.I18, ReportRows.I19, ReportRows.I20, ReportRows.I21,ReportRows.I22, ReportRows.I23, ReportRows.I24, ReportRows.I25, ReportRows.I26,ReportRows.I27, ReportRows.I28, ReportRows.I29, ReportRows.I30, ReportRows.I31,ReportRows.I32, ReportRows.I33, ReportRows.I34, ReportRows.I35, ReportRows.I36,ReportRows.I37, ReportRows.I38, ReportRows.I39, ReportRows.I40, ReportRows.I41,ReportRows.I42, ReportRows.I43, ReportRows.I44, ReportRows.I45, ReportRows.I46,ReportRows.I47, ReportRows.I48, ReportRows.I49, ReportRows.I50  FROM ReportRows INNER JOIN ReportParm ON ReportRows.id = ReportParm.RowId order by ReportRows.ReportId,ReportRows.OrderNo ";
			model.ReportParm_View = comService.GetDataTable(sql);

			sql = "select * from ReportIndexCol  order by ReportId,OrderNo";
			model.ReportIndexCol = comService.GetDataTable(sql);

			sql = "select * from ReportIndexColParm";
			model.ReportIndexColParm = comService.GetDataTable(sql);

			sql = "select * from ReportIndexSearch order by ReportId,OrderNo";
			model.ReportIndexSearch = comService.GetDataTable(sql);

			sql = "select * from ReportIndexOrder order by ReportId,OrderNo";
			model.ReportIndexOrder = comService.GetDataTable(sql);

			model.allView = comService.GetViewList();

			DataTable dt = DataTableHelp.NewViewFieldDt();
			foreach (DataRow drView in model.allView.Rows)
			{
				foreach (DataRow dr in comService.GetTbFieldList(drView["Name"].ToString()).Rows)
				{
					DataRow row = dt.NewRow();
					row["View_Name"] = drView["Name"].ToString();
					row["COLUMN_NAME"] = dr["COLUMN_NAME"].ToString();
					row["DATA_type"] = dr["DATA_type"].ToString();
					dt.Rows.Add(row);
				}
			}
			model.ViewField = dt;

			return model;
		}

		public static AppSetEntity GetSetModel(string siteRoot, string dbType)
		{
			string templetFilePath = "";

			AppSetEntity model = new AppSetEntity();

			model.URL = Think9.Services.Base.Configs.GetValue("YoursLCSA");
			model.AppId = Think9.Services.Base.Configs.GetValue("YoursLCAppId");
			model.PassWord = Think9.Services.Base.Configs.GetValue("YoursLCPassWord");

			model.DbType = dbType;

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Dapper\\BuildCode.txt";
			model.BuildCode = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Dapper\\ControllerCs.txt";
			model.ControllerCs = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Dapper\\ControllerGridCs.txt";
			model.ControllerGridCs = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Dapper\\ControllerPUCs.txt";
			model.ControllerPUCs = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Dapper\\GridServiceCs.txt";
			model.GridServiceCs = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Dapper\\ServiceCs.txt";
			model.ServiceCs = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			//Layui
			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Layui\\DetailCshtml.txt";
			model.DetailCshtml = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Layui\\FormCshtml.txt";
			model.FormCshtml = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Layui\\FormGrid2Cshtml.txt";
			model.FormGrid2Cshtml = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Layui\\FormGridCshtml.txt";
			model.FormGridCshtml = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Layui\\FormNoGridCshtml.txt";
			model.FormNoGridCshtml = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Layui\\ListCshtml.txt";
			model.ListCshtml = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Layui\\PuCshtml.txt";
			model.PuCshtml = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Layui\\PuCshtmlDict.txt";
			model.PuCshtmlDict = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Layui\\PuCshtmlTree.txt";
			model.PuCshtmlTree = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Layui\\SelfJS.txt";
			model.SelfJS = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Rdlc\\TableRdlc.txt";
			model.TableRdlc = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Rdlc\\ListRdlc.txt";
			model.ListRdlc = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Rdlc\\ReportRdlc.txt";
			model.ReportRdlc = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Rdlc\\ReportRdlc2.txt";
			model.ReportRdlc2 = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Dapper\\ReportControllerCs.txt";
			model.ReportControllerCs = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Dapper\\ReportServiceCs.txt";
			model.ReportServiceCs = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Layui\\ReportCshtml.txt";
			model.ReportCshtml = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Dapper\\ReportControllerCs2.txt";
			model.ReportControllerCs2 = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Dapper\\ReportServiceCs2.txt";
			model.ReportServiceCs2 = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			templetFilePath = siteRoot + "\\wwwroot\\AppTempletFile\\Layui\\ReportCshtml2.txt";
			model.ReportCshtml2 = Think9.Util.Helper.FileHelper.FileToString(templetFilePath);

			return model;
		}
	}
}